#include "communication_manager.h"


Message* CommunicationManager::CreateMessage(const std::string& name, const std::string& typeName)
{
    Message* message = NULL;
    const Descriptor* descriptor = DescriptorPool::generated_pool()->FindMessageTypeByName(typeName);
    if(descriptor != NULL)
    {
        const Message* prototype = MessageFactory::generated_factory()->GetPrototype(descriptor);
        if (prototype != NULL)
        {
            message = prototype->New();
        }
        else
        {
             WARNING("creat message prototyper is fail! typeName[%s]", typeName.c_str());
        }
    }
    else
    {
        WARNING("creat message descriptor is fail! typeName[%s]", typeName.c_str());
    }
    if(message != NULL)
    {
        m_MessageMap[name] = message;
    }
    return message;
}

Message* CommunicationManager::GetMessage(const std::string& name)
{
    std::map<std::string, Message*>::iterator iter = m_MessageMap.find(name);
    if(iter == m_MessageMap.end())
        return NULL;
    else
        return iter->second;
}

int CommunicationManager::RemoveMessage(const std::string& name)
{
    std::map<std::string, Message*>::iterator iter = m_MessageMap.find(name);
    if(iter == m_MessageMap.end())
    {
         WARNING("remove message is not exist! name[%s]", name.c_str());
         return -1;
    }
    else
    {
        m_MessageMap.erase(iter);
        return 0;
    }
    return 0;
}















/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
